-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Extend QueryStability
to handle IntoIterator
implementations
#139345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
7c51ef9
to
1a2c5a3
Compare
This comment has been minimized.
This comment has been minimized.
Apologies, I was not aware of the But in your opinion, @fmease, should I keep the |
I think it would make sense to merge Note that |
I think this is good to go, @fmease. Thank you for your suggestions. EDIT: Nits are welcome, BTW. |
@fmease Are there any questions I could answer about this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay. I've had an initial scan. Not all of my comments are actionable unfortunately, I'll try to come back to it later.
compiler/rustc_lint/src/internal.rs
Outdated
let Some(into_iterator_def_id) = cx.tcx.get_diagnostic_item(sym::IntoIterator) else { | ||
return; | ||
}; | ||
if expr.span.from_expansion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suppresses the lint from firing for code like this?
fn iter<T>(x: impl IntoIterator<Item = T>) = impl Iterator<Item = T> { x.into_iter() }
macro_rules! iter { ($e:expr) => { iter($e) } }
fn take(map: std::collections::HashMap<i32, i32>) { _ = iter!(map); }
I think we should fire regardless. Internal lints can be a lot more aggressive than Clippy lints. There's a reason why rustc::potential_query_instability
is marked report_in_external_macro: true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without that condition, an additional warning is produced here:
rust/tests/ui-fulldeps/internal-lints/query_stability.rs
Lines 22 to 23 in dfec3c0
for _ in x {} | |
//~^ ERROR using `into_iter` |
+ error: using `into_iter` can result in unstable query results
+ --> $DIR/query_stability.rs:22:14
+ |
+ LL | for _ in x {}
+ | ^
+ |
+ = note: if you believe this case to be fine, allow this lint and add a comment explaining your rationale
Not linting expanded code seemed like the most straightforward way of avoiding the duplicate warnings.
Would you prefer that the lint check the context in which the expression appears, e.g., something along these lines? https://doc.rust-lang.org/beta/nightly-rustc/src/clippy_utils/higher.rs.html#34-54
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should lint in expansions (generally), however, we could avoid looking for IntoITerator
trait builds in case the lint already fired for the method call itself. i.e. if wee lint because we resolved to a method with the attribute, don't check whether that method also has a relevant IntoIterator
bound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, i think we can merge this lint into one
if let Some((callee_def_id, span, generic_args, _recv, _args)) =
get_callee_span_generic_args_and_args(cx, expr)
first checking whether the method/call itself is marked as query unstable, then check whether it has a where-bound which relies on an unstable trait impl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not following. What do you mean by "merge this lint into one"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rn u lint in two cases
- the expr is a method/call
- we can resolve it to a method with a
#[rustc_lint_query_instability]
attribute
- we can resolve it to a method with a
- the expr is a method
- the method has a where-bound which is proven by an impl with a
#[rustc_lint_query_instability]
attribute.
- the method has a where-bound which is proven by an impl with a
Why not do
- the expr is a method/call
- we can resolve it to a method with a
#[rustc_lint_query_instability]
attribute - the method has a where-bound which is proven by an impl with a
#[rustc_lint_query_instability]
attribute.
- we can resolve it to a method with a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please tell me if what I pushed is what you had in mind.
compiler/rustc_lint/src/internal.rs
Outdated
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.typing_env(), def_id, args) | ||
impl<'tcx> LateLintPass<'tcx> for QueryStability { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be awesome if we could somehow unify typeck_results_of_method_fn
and get_callee_generic_args_and_args
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit ae437dd.
This comment has been minimized.
This comment has been minimized.
2a4d30f
to
ae437dd
Compare
This comment has been minimized.
This comment has been minimized.
Apologies. The force push was to remove an unnecessary change I included accidentally. EDIT: Note that another commit (085312b re #139345 (comment)) has been since been pushed. |
Hi @fmease, it looks like the author has responded to all of your review feedback so this is ready for another look when you get a chance. Thanks! |
r? rust-lang/compiler |
compiler/rustc_lint/src/internal.rs
Outdated
use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy}; | ||
use rustc_hir::{Expr, ExprKind, HirId}; | ||
use rustc_middle::ty::{ | ||
self, ClauseKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty as MiddleTy, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self, ClauseKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty as MiddleTy, | |
self, ClauseKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty, |
please avoid importing hir::Ty
instead. We generally always uses Ty
for ty::Ty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me.
610cb4d
to
cf80434
Compare
This comment has been minimized.
This comment has been minimized.
I'm trying to rebase onto master to eliminate the build failure. Please forgive the impending churn. |
cf80434
to
ba9c93e
Compare
Some changes occurred in compiler/rustc_codegen_ssa |
lgtm @bors delegate+ |
please squash your commits though :3 |
Fix adjacent code Fix duplicate warning; merge test into `tests/ui-fulldeps/internal-lints` Use `rustc_middle::ty::FnSig::inputs` Address two review comments - rust-lang#139345 (comment) - rust-lang#139345 (comment) Use `Instance::try_resolve` Import `rustc_middle::ty::Ty` as `Ty` rather than `MiddleTy` Simplify predicate handling Add more `#[allow(rustc::potential_query_instability)]` following rebase Remove two `#[allow(rustc::potential_query_instability)]` following rebase Address review comment Update compiler/rustc_lint/src/internal.rs Co-authored-by: lcnr <[email protected]>
49e13a7
to
c3c2c23
Compare
Extend `QueryStability` to handle `IntoIterator` implementations This PR extends the `rustc::potential_query_instability` lint to check values passed as `IntoIterator` implementations. Full disclosure: I want the lint to warn about this line (please see rust-lang#138871 for why): https://github.com/rust-lang/rust/blob/aa8f0fd7163a2f23aa958faed30c9c2b77b934a5/src/librustdoc/json/mod.rs#L261 However, the lint warns about several other lines as well. Final note: the functions `get_callee_generic_args_and_args` and `get_input_traits_and_projections` were copied directly from [Clippy's source code](https://github.com/rust-lang/rust/blob/4fd8c04da0674af2c51310c9982370bfadfa1b98/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs#L445-L496).
Rollup of 11 pull requests Successful merges: - #139345 (Extend `QueryStability` to handle `IntoIterator` implementations) - #144838 (Fix outdated doc comment) - #145206 (Port `#[custom_mir(..)]` to the new attribute system) - #145208 (Implement declarative (`macro_rules!`) derive macros (RFC 3698)) - #145309 (Fix `-Zregparm` for LLVM builtins) - #145355 (Add codegen test for issue 122734) - #145420 (cg_llvm: Use LLVM-C bindings for `LLVMSetTailCallKind`, `LLVMGetTypeKind`) - #145451 (Add static glibc to the nix dev shell) - #145460 (Speedup `copy_src_dirs` in bootstrap) - #145476 (Fix typo in doc for library/std/src/fs.rs#set_permissions) - #145485 (Fix deprecation attributes on foreign statics) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 11 pull requests Successful merges: - #139345 (Extend `QueryStability` to handle `IntoIterator` implementations) - #144838 (Fix outdated doc comment) - #145206 (Port `#[custom_mir(..)]` to the new attribute system) - #145208 (Implement declarative (`macro_rules!`) derive macros (RFC 3698)) - #145309 (Fix `-Zregparm` for LLVM builtins) - #145355 (Add codegen test for issue 122734) - #145420 (cg_llvm: Use LLVM-C bindings for `LLVMSetTailCallKind`, `LLVMGetTypeKind`) - #145451 (Add static glibc to the nix dev shell) - #145460 (Speedup `copy_src_dirs` in bootstrap) - #145476 (Fix typo in doc for library/std/src/fs.rs#set_permissions) - #145485 (Fix deprecation attributes on foreign statics) r? `@ghost` `@rustbot` modify labels: rollup
Failed in rollup: #145488 (comment) This might need a re-bless. @bors r- |
@lcnr The failure occurs in this test:
This suggests to me that the stage 1 compiler is failing because it doesn't have the lint enhancement. If that's correct, is there something I need to do to prevent the stage 1 compiler from running that test? EDIT: Also, I can reproduce the failure locally. I had been running with |
Because this test is really testing compiler behaviour, and is only in ui-fulldeps because it happens to need Therefore, I think it might be appropriate to add an |
I added that to |
This comment has been minimized.
This comment has been minimized.
7265541
to
3b0ff9c
Compare
I'm experiencing some weirdness with the I don't want to abuse delegate privileges. So could I trouble you to give this another look, @lcnr? |
This PR extends the
rustc::potential_query_instability
lint to check values passed asIntoIterator
implementations.Full disclosure: I want the lint to warn about this line (please see #138871 for why):
rust/src/librustdoc/json/mod.rs
Line 261 in aa8f0fd
However, the lint warns about several other lines as well.
Final note: the functions
get_callee_generic_args_and_args
andget_input_traits_and_projections
were copied directly from Clippy's source code.